package com.hongyuan.sellfitness.ui.person.view_mode;

import android.app.Application;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.hongyuan.mvvmhabitx.binding.command.BindingAction;
import com.hongyuan.mvvmhabitx.binding.command.BindingCommand;
import com.hongyuan.mvvmhabitx.bus.event.SingleLiveEvent;
import com.hongyuan.sellfitness.base.Constants;
import com.hongyuan.sellfitness.base.CustomViewModel;
import com.hongyuan.sellfitness.data.MyRepository;
import com.hongyuan.sellfitness.data.md5.BaseUtil;
import com.hongyuan.sellfitness.ui.main.activity.PayActivity;
import com.hongyuan.sellfitness.ui.main.beans.V3SuccessBeans;
import com.hongyuan.sellfitness.ui.person.beans.KeFuBean;
import com.hongyuan.sellfitness.ui.person.beans.MyOrderDeatilBean;

import java.util.ArrayList;
import java.util.List;

public class MyOrdersDetailViewModel extends CustomViewModel<MyRepository> {

    public ObservableField<MyOrderDeatilBean.InfoBean> entity = new ObservableField<>();
    public ObservableField<KeFuBean> callKfTel = new ObservableField<>();

    public MyOrdersDetailViewModel(@NonNull Application application, MyRepository model) {
        super(application, model);
    }

    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        //取消订单
        public SingleLiveEvent<Void> cancelOrder = new SingleLiveEvent<>();
    }

    //联系客服
    public BindingCommand telKf = new BindingCommand(new BindingAction() {
        @Override
        public void call() {

            getUC().getCallTel().setValue(callKfTel.get().getInfo().getTel());
        }
    });

    //取消订单
    public BindingCommand cancel = new BindingCommand(new BindingAction() {
        @Override
        public void call() {

            uc.cancelOrder.call();
        }
    });

    //去支付订单
    public BindingCommand goPay = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            Bundle bundle = new Bundle();
            bundle.putString("payPrice",entity.get().getO_money());
            bundle.putString("payOid",String.valueOf(entity.get().getO_id()));
            bundle.putSerializable("successBeans",getSuccessBeans());

            startActivity(PayActivity.class,bundle);
        }
    });

    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);


        if(code == Constants.GET_ORDER_INFO){
            MyOrderDeatilBean.InfoBean bean = ((MyOrderDeatilBean)dataBean).getInfo();

            entity.set(bean);
        }

        if(code == Constants.CANCEL_ORDER){
            finish();
        }

        if(code == Constants.GET_ONLINE_KF){
            KeFuBean keFuBean = (KeFuBean)dataBean;
            callKfTel.set(keFuBean);
        }
    }

    /*
     * 组装订单显示信息
     * */
    private V3SuccessBeans getSuccessBeans(){
        V3SuccessBeans beans = new V3SuccessBeans();
        List<V3SuccessBeans.ItemConten> list = new ArrayList<>();

        V3SuccessBeans.ItemConten itemConten;

        if(entity.get().getO_type_code().equals("o_card")){
            beans.setType(V3SuccessBeans.TYPE.BUYCARD);
            beans.setTitleText("购卡订单");
            beans.setShowText("购买成功");
            beans.setBtn2Text("完成");

            itemConten = new V3SuccessBeans.ItemConten();
            itemConten.setContent(entity.get().getO_name());
            itemConten.setItemTitle("会籍卡名称:");
            list.add(itemConten);
        }else if(entity.get().getO_type_code().equals("o_pric")){
            beans.setType(V3SuccessBeans.TYPE.PRIVITECLASS);
            beans.setTitleText("私教课订单");
            beans.setShowText("购买成功");
            beans.setBtn2Text("完成");

            itemConten = new V3SuccessBeans.ItemConten();
            itemConten.setContent(entity.get().getO_name()+" / 一对一私教课");
            itemConten.setItemTitle("课程类型:");
            list.add(itemConten);

            itemConten = new V3SuccessBeans.ItemConten();
            itemConten.setContent(entity.get().getO_num()+"节");
            itemConten.setItemTitle("数量:");
            list.add(itemConten);

            itemConten = new V3SuccessBeans.ItemConten();
            itemConten.setContent("¥"+ BaseUtil.getNoZoon(entity.get().getO_money()));
            itemConten.setItemTitle("总价:");
            list.add(itemConten);
        }


        itemConten = new V3SuccessBeans.ItemConten();
        itemConten.setContent("￥"+ BaseUtil.getNoZoon(entity.get().getO_price()));
        itemConten.setItemTitle("价格:");
        list.add(itemConten);

        if(BaseUtil.isValue(entity.get().getO_coupon_money()) && Double.valueOf(entity.get().getO_coupon_money()) > 0){
            itemConten = new V3SuccessBeans.ItemConten();
            itemConten.setContent("-¥"+BaseUtil.getNoZoon(entity.get().getO_coupon_money()));
            itemConten.setItemTitle("优惠:");
            list.add(itemConten);
        }
        beans.setItemContens(list);

        return beans;
    }


}
